package com.hxl.tech.gateway.auth;


import com.hxl.tech.gateway.auth.config.OpenApiConfig;
import com.hxl.tech.gateway.auth.model.Resource;
import com.hxl.tech.gateway.auth.service.ResourceService;
import com.hxl.tech.gateway.auth.util.AppInitUtil;
import com.hxl.tech.gateway.common.constant.AppConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


/**
 * 初始化刷新Redis资源
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class InitRunner implements ApplicationRunner {

    private final ResourceService resourceService;
    private final AppInitUtil appInitUtil;

    @Override
    public void run(ApplicationArguments args){

        // 读取默认配置
        String shareUrl = OpenApiConfig.getOpenApiUrl();
        com.hxl.tech.gateway.auth.model.Resource resource = new com.hxl.tech.gateway.auth.model.Resource();
        resource.setId(new Resource.Id(AppConstant.RESOURCE_MERCHANT_DEFAULT, AppConstant.RESOURCE_OPEN_URL));
        resource.setResourceValue(shareUrl);
        resourceService.saveOrUpdate(resource);

        appInitUtil.freshOpenUrl();
        appInitUtil.freshApplication();
        appInitUtil.freshApi();
        log.info("完成初始化刷新网关资源");

        appInitUtil.freshLimitIp();
        appInitUtil.freshLimitRate();
        log.info("完成初始化刷新网关限流策略");
    }
}
